package com.tarena.lbs.business.dao.repository.impl;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.business.dao.mapper.StoreMapper;
import com.tarena.lbs.business.dao.repository.StoreRepository;
import com.tarena.lbs.business.pojo.po.StorePO;
import com.tarena.lbs.business.pojo.query.AreaStoreQuery;
import com.tarena.lbs.business.pojo.query.StoreQuery;
import com.tarena.lbs.business.pojo.vo.StoreVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
@Slf4j
@Repository
public class StoreRepositoryImpl implements StoreRepository {
    @Autowired
    private StoreMapper storeMapper;
    @Override
    public PageInfo<StorePO> pagesList(StoreQuery query) {
        log.debug("查询门店数据访问参数：{}", query);
        QueryWrapper<StorePO> queryWrapper = assembleStoreQuery(query);
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<StorePO> pos = storeMapper.selectList(queryWrapper);
        log.debug("查询门店数据访问返回：{}", pos);
        return new PageInfo<>(pos);
    }
    private QueryWrapper<StorePO> assembleStoreQuery(StoreQuery query) {
        QueryWrapper<StorePO> queryWrapper = new QueryWrapper<>();

        // 门店名称模糊查询
        if (StringUtils.isNotBlank(query.getStoreName())) {
            queryWrapper.like("store_name", query.getStoreName());
        }

        // 门店电话模糊查询
        if (StringUtils.isNotBlank(query.getStorePhone())) {
            queryWrapper.like("store_phone", query.getStorePhone());
        }

        // 负责人模糊精确查询
        if (StringUtils.isNotBlank(query.getStoreHeadPhone())) {
            queryWrapper.like("store_head_phone", query.getStoreHeadPhone());
        }

        // 省份筛选
        if (query.getProvinceId() != null) {
            queryWrapper.eq("province_id", query.getProvinceId());
        }

        // 城市筛选
        if (query.getCityId() != null) {
            queryWrapper.eq("city_id", query.getCityId());
        }

        // 区域筛选
        if (query.getAreaId() != null) {
            queryWrapper.eq("area_id", query.getAreaId());
        }

        // 门店状态筛选
        if (query.getStoreStatus() != null) {
            queryWrapper.eq("store_status", query.getStoreStatus());
        }

        // 时间范围查询（更新时间）
        if (query.getStartingTime() != null && query.getEndTime() != null) {
            queryWrapper.between("create_time", query.getStartingTime(), query.getEndTime());
        } else if (query.getStartingTime() != null) {
            queryWrapper.ge("create_time", query.getStartingTime());
        } else if (query.getEndTime() != null) {
            queryWrapper.le("create_time", query.getEndTime());
        }

        return queryWrapper;
    }
    @Override
    public void addStore(StorePO po) {
        if (po.getId() != null) {
            storeMapper.updateById(po);
        }else {
            storeMapper.insert(po);
        }
    }

    @Override
    public List<StoreVO> listById(AreaStoreQuery query) {
        List<StoreVO> pos=storeMapper.selectListByCity(query);
        return pos;
    }
    @Override
    public StorePO getStoreById(Integer id) {
        log.debug("查询门店数据访问参数：{}", id);
        StorePO po=storeMapper.selectById(id);
        log.debug("查询门店数据访问返回：{}", po);
        return po;
    }
    @Override
    public void deleteStore(Integer id) {
        log.debug("删除门店数据访问参数：{}", id);
        storeMapper.deleteById(id);
        log.debug("删除门店数据访问返回：{}", "删除成功");
    }
}
